import { Request, Response } from 'express';

export function cors(req: Request, res: Response, next: () => void) {
    const origin = req.get('Origin');
    // 判断是不是来自跨域的请求
    if (origin !== undefined) {
        res.set({
            'Access-Control-Allow-Origin': "*",
            'Access-Control-Allow-Methods': 'GET, HEAD, PUT, PATCH, POST, DELETE',
            'Access-Control-Allow-Headers': 'origin, x-requested-with, content-type',
        });
        // 判断是不是预检请求
        if (req.method === 'OPTIONS') {
            res.status(200).end();
            return;
        }
    }
    next();
}
